package com.tutucms.service.util;

import com.alibaba.fastjson.JSONObject;
import com.tutucms.common.util.CharUtil;
import com.tutucms.common.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by wangxudong on 2020/03/17.
 *
 * @version: 1.0
 * @modified :
 */
@Slf4j
public class OssStorageUtil {

    public static Integer[] getPicInfo(String url) {
        if (CharUtil.isEmpty(url)) {

            return null;
        }
        try {
            String s = HttpClientUtil.doGet(url + "?x-oss-process=image/info");
            JSONObject jsonObject = JSONObject.parseObject(s);
            JSONObject imageHeight = (JSONObject) jsonObject.get("ImageHeight");
            Integer height = CharUtil.stringParsInt(imageHeight.get("value").toString());
            JSONObject imageWidth = (JSONObject) jsonObject.get("ImageWidth");
            Integer width = CharUtil.stringParsInt(imageWidth.get("value").toString());

            return new Integer[]{height, width};
        } catch (Exception e) {

            log.error(e.getMessage(), e);

            return null;
        }
    }

    // 获取视频的第一秒帧
    public static String getVideoImgUrl(String videoUrl){

        return videoUrl + "?x-oss-process=video/snapshot,t_1,f_jpg,w_0,h_0";
    }
}
